// pages/my/myExp/myExp.js
const OPENID = wx.getStorageSync('userOpenid').OPENID;
const db = wx.cloud.database();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tempWorkData: [],
    show: false,
    showId: "",
    showState: 0,
    showTitle: "0",
    showIndex:0,
    workData: [
      {
        _id: 1,
        state: 1,
        sorting: 1,
        title: "茂南区家教",
        //red green blue yellow
        tag: [{
          tagData: "月薪",
          tagColor: "green"
        },
        {
          tagData: "高分",
          tagColor: "yellow"
        },
        ],
        minContent: "招聘 茂南区家教 要求:学生",
        wage: '月薪 1000-2000元',
        address: '地址:茂南区194号',
        number: {
          applications: 2,
          restrictions: 10,
        },
        deadlineDate: "2022-08-12"
      },
      {
        _id: 2,
        state: 0,
        sorting: 1,
        title: "电白区家教",
        tag: [{
          tagData: "月结",
          tagColor: "green"
        },
        {
          tagData: "高分",
          tagColor: "yellow"
        },
        {
          tagData: "多人报告",
          tagColor: "blue"
        },
        {
          tagData: "高薪",
          tagColor: "yellow"
        }
        ],
        minContent: "招聘 电白区家教 要求:学生",
        wage: '100-200 人民币/日',
        address: '地址:电白区北路177号',
        number: {
          applications: 2,
          restrictions: 10,
        },
        deadlineDate: "2022-08-12"
      },
      {
        _id: 3,
        state: 2,
        sorting: 1,
        title: "茂南区家教",
        tag: [{
          tagData: "月薪",
          tagColor: "green"
        },
        {
          tagData: "高分",
          tagColor: "red"
        },
        ],
        minContent: "招聘 茂南区家教 要求:学生",
        wage: '月薪 1000-2000元',
        address: '地址:茂南区194号',
        number: {
          applications: 2,
          restrictions: 10,
        },
        deadlineDate: "2022-08-12"
      }
    ]
  },

  onLoad: function (options) {
    var applyList = []
    var workData = []
    db.collection('applyerApplyData').where({
      _openid: OPENID
    })
      .get({
        success: res => {
          applyList = res.data[0].applyList

          try {
            var idWorkData
            for (var i = 0; i < applyList.length; i++) {
              idWorkData = applyList[i].idWorkData

              db.collection('workData').doc(idWorkData).get({
                success: res => {
                  workData = this.data.tempWorkData
                  workData.push(res.data)

                  this.setData({
                    tempWorkData: workData
                  })
                  if (this.data.tempWorkData.length == applyList.length) {
                    for (var j = 0; j < applyList.length; j++) {
                      workData[j].state = applyList[j].stateApply
                    }
                    this.setData({
                      workData: workData
                    })
                    console.log(this.data.workData)
                  }
                }
              })

            }
          } catch (e) {
            console.log(e.message)
          }

        }
      })

  },

  onToHome: function (e) {
    wx.switchTab({
      url: '../../../pages/home/index/index'
    })
  },

  showPopup: function (e) {
    this.setData({
      show: true,
      showId: e.currentTarget.dataset.showid,
      showState: e.currentTarget.dataset.state,
      showTitle: e.currentTarget.dataset.title,
      showIndex :e.currentTarget.dataset.index
    });
    console.log(e.currentTarget.dataset)
  },

  onClose() {
    this.setData({ show: false });
  },

  onToApprovalProcess: function (e) {//查看审核
    const transmitData = {
      showState: this.data.showState,
      showTitle: this.data.showTitle
    }

    wx.navigateTo({
      url: 'approvalProcess/approvalProcess',
      success: function (res) {
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('transmitData', {
          data: transmitData
        })
      }
    })
  },

  onToWorkContent: function (e) {//查看详情
    const workDataId = this.data.showId
    wx.navigateTo({
      url: '../../home/workContent/workContent',
      success: function (res) {
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('workDataId', {
          data: workDataId
        })
      }
    })
  },

  onToManagement: function (e) {//取消申请
    const showIndex = this.data.showIndex
    var workData = this.data.workData
    workData.splice(showIndex, 1)
    this.setData({
      workData:workData
    })
    this.onClose()
    // db.collection('applyerApplyData').where({
    //   _openid: OPENID
    // })
    //   .get({
    //     success: res => {
    //       console.log(res.data)
    //       var applyList = res.data[0].applyList
    //       var _id = res.data[0]._id
    //       var idApplyWorkData

    //       console.log(applyList)
    //       try{
    //         for (var i = 0; i < applyList; i++) {
    //           if (applyList[i].idWorkData == workDataId) {
    //             idApplyWorkData = applyList[i].idApplyWorkData
    //             applyList.splice(i, 1)
    //           }
    //           console.log(applyList)
    //         }
    //       }catch(e){
    //         console.log(e.message)
    //       }
        

    //       db.collection('applyerApplyData').doc(_id).update({
    //         data: {
    //           applyList: applyList
    //         },
    //         success: function (res) {
    //           console.log(res.data)

    //           db.collection('applyWorkData').doc(idApplyWorkData).get({
    //             success: res => {
    //               applyList = res.data.applyList

    //               for (var j = 0; j < applyList; j++) {
    //                 if (applyList[j].openIdApplyer == OPENID) {
    //                   applyList.splice(j, 1)
    //                 }
    //               }

    //               db.collection('applyWorkData').doc(idApplyWorkData).update({
    //                 data: {
    //                   applyList: applyList
    //                 },
    //                 success: function (res) {
    //                   console.log(res)
    //                 }
    //               })

    //             }
    //           })

    //         }
    //       })

    //     }
    //   })

  }

})